package net.xdclass.config;

import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description redission 配置
 * @Author lihw
 * @Date 2023/4/17 9:38
 */
@Configuration
public class RedissonConfiguration {

    @Value("${spring.redis.host}")
    private String redisHost;

    @Value("${spring.redis.port}")
    private String redisPort;

    @Value("${spring.redis.password}")
    private String redisPwd;


    /**
     * 配置分布式锁 redisson （单机模式）
     * @return
     */
    @Bean
    public RedissonClient redissonClient() {
        Config config = new Config();

        redisPwd = StringUtils.isEmpty(redisPwd) ? null : redisPwd;
        // 单机方式
        config.useSingleServer()
                .setPassword(redisPwd)
                .setAddress("redis://" + redisHost + ":" + redisPort);
        // 集群
       /* config.useSingleServer()
                .setPassword(redisPwd)
                .setAddress("redis://" + redisHost + ":" + redisPort,
                "redis://" + redisHost + ":" + redisPort);*/
        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }



}
